#encoding: utf-8


from random import randint
from time import time, sleep


# def download_task(fliename):
# 	print('开始下载。。。%s' % fliename)
# 	time_to_download = randint(5, 10)
# 	sleep(time_to_download)

# 	print('%s 下载完成！ 耗费了%d秒' % (fliename, time_to_download))

# def main():
# 	start = time()
# 	download_task('python从入门到住院.pdf')
# 	download_task('next')
# 	end = time()
# 	print('一共耗费了 %.2f 秒' % (end - start))

# ==========================

# from multiprocessing import Process
# from os import getpid

# def  download_task(fliename):
# 	print('启动下载进程，进程号 %d' % getpid())
# 	print('开始下载---- %s' % fliename)
# 	time_to_download = randint(5, 10)
# 	sleep(time_to_download)
# 	print('下载完成 %s， 耗费时间 %d 秒' % (fliename, time_to_download))

# def main():
# 	start = time()
# 	p1 = Process(target = download_task, args =('111111.pdf', ))
# 	p1.start()
# 	p2 = Process(target = download_task, args = ('222222.avi', ))
# 	p2.start()
# 	p1.join()
# 	p2.join()
# 	end = time()
# 	print('总共耗费了 %.2f 秒' % (end - start))


# ==========================
from threading import Thread, Lock
# def download(filename):
# 	print('开始下载%s' % filename)
# 	time_to_download = randint(3, 5)
# 	sleep(time_to_download)
# 	print(' %s 下载完成 ， 耗费时间 %d 秒' % (filename, time_to_download))

# def main():
# 	start = time()
# 	t1 = Thread(target=download, args = ('11111111.pdf', ))
# 	t1.start()
# 	t2 = Thread(target=download, args = ('222222222.avi', ))
# 	t2.start
# 	t1.join()
# 	t2.join()
# 	end = time()
# 	print('总共耗费了 %.3f 秒' % (end - start))

# ===============================
# 可以直接使用threading模块的Thread类来创建线程，但是我们之前讲过一个非常重要的概念叫“继承”，
# 我们可以从已有的类创建新类，因此也可以通过继承Thread类的方式来创建自定义的线程类，然后再创建线程对象并启动线程。

# class DownloadTask(Thread):
# 	"""docstring for ClassName"""
# 	def __init__(self, filename):
# 		super(DownloadTask, self).__init__()
# 		self._filename = filename
		
# 	def run(self):
# 		print('开始下载%s...' % self._filename)
# 		time_to_download = randint(2, 4)
# 		sleep(time_to_download)
# 		print('%s 下载完成！ 耗费了 %d 秒 ' % (self._filename, time_to_download))
# 		pass

# def main():
# 	start = time()
# 	t1 = DownloadTask('2222222222.pdf')
# 	t1.start()

# 	t2 = DownloadTask('3333333333.avi')
# 	t2.start()

# 	t1.join()
# 	t2.join()
# 	end = time()
# 	print('一共耗费了 %.2f 秒' % (end - start))
# 	pass


class Account(object):
	"""docstring for Account"""
	def __init__(self):
		super(Account, self).__init__()
		self._balance = 0
		self._lock = Lock()
		
	def deposit(self, money):
		self._lock.acquire()
		try:
			new_balance = self._balance + money
			sleep(0.01)
			self._balance = new_balance
		finally:
			self._lock.release()

	def balance(self):
		return self._balance
		pass


class AddMoneyThread(Thread):
	"""docstring for AddMoneyThread"""
	def __init__(self, account, money):
		super(AddMoneyThread, self).__init__()
		self._account = account
		self._money = money
		
	def run(self):
		self._account.deposit(self._money)

def main():
	account = Account()
	threads = []
	for _ in range(100):
		t = AddMoneyThread(account, 1)
		threads.append(t)
		t.start()
	for t in threads:
		t.join()
	print('账户余额为： %d 元' % account._balance)






if __name__ == '__main__':
	main()



